package com.wifi.eshipewifi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wifi.BaseApplication;
import com.wifi.imp.MessageListener;
import com.wifi.imp.MessageSend;
import com.wifi.utils.WifiUtils;
import com.wifi.view.ToggleView;

public class NetWorkSetting extends Activity implements View.OnClickListener,MessageListener {

    private String[] allProv={"全国","北京","天津","河北","山西","内蒙古","辽宁","吉林"
            ,"黑龙江","上海","江苏","浙江","安徽","福建","江西"
            ,"山东","河南","湖北","湖南","广东","广西","海南","重庆"
            ,"四川","贵州","云南","西藏"
            ,"陕西","甘肃","青海","宁夏","新疆","香港","澳门","台湾"};//所有的省

    RelativeLayout rl_package;
    RelativeLayout rl_gps_map;
    LinearLayout ll_setting;
    LinearLayout ll_sim_auto;
    LinearLayout ll_sim1;
    LinearLayout ll_sim2;
    LinearLayout ll_background;
    ImageButton ib_back;
    TextView tv_include_tittle;
    TextView tv_sign_name;
    TextView tv_gps;
    TextView tv_auto;
    TextView tv_sim2;
    TextView tv_sim1;
//    TextView tv_way_name;
    ImageView tv_way_name;
    ImageView iv_auto;
    ImageView iv_sim1;
    ImageView iv_sim2;
    TextView tv_sign_state;
    ToggleView iv_toright2;
    BaseApplication application;
    String TAG="NetWorkSetting";
    boolean isClose=false;
    boolean isSimAutoClose=false;
    boolean isSim1Close=false;
    boolean isSim2Close=false;
    SharedPreferences sp;
    ProgressBar pb_conn;
    Double gps1;
    Double gps2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net_work_setting);

        initView();
        application = (BaseApplication) getApplication();
        //信息填充
        sp= getSharedPreferences("package", Activity.MODE_PRIVATE);



        boolean isNetWockOpen = sp.getBoolean("isNetWockOpen", false);
        String yys = sp.getString("YYS", "中国联通");
        tv_sign_name.setText(yys);
        if(isNetWockOpen){
            tv_way_name.setBackgroundResource(R.drawable.open);
            isClose=false;
        }else {
            tv_way_name.setBackgroundResource(R.drawable.close);
            isClose=true;
        }

        //设置快关4G--静态
        tv_way_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isClose){//点击打开
                    application.sendMsg("$up4g;");
                    pb_conn.setVisibility(View.VISIBLE);
                    tv_way_name.setClickable(false);
                }else {//点击关闭
                    application.sendMsg("$down4g;");
                    pb_conn.setVisibility(View.VISIBLE);
                    tv_way_name.setClickable(false);
                }
            }
        });

        //设置SIM卡
        iv_auto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSimAutoClose){//点击打开
                    application.sendMsg("$change_SIM;auto;");
                    iv_auto.setClickable(false);
                }else {//点击关闭
                    application.sendMsg("$change_SIM;sim1;");
                    iv_auto.setClickable(false);
                }
            }
        });

        //设置SIM卡
        iv_sim1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSim1Close){//点击打开
                    application.sendMsg("$change_SIM;sim1;");
                    iv_sim1.setClickable(false);
                }else {//点击关闭
                    application.sendMsg("$change_SIM;sim2;");
                    iv_sim1.setClickable(false);
                }
            }
        });
        //设置SIM卡
        iv_sim2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSim2Close){//点击打开
                    application.sendMsg("$change_SIM;sim2;");
                    iv_sim2.setClickable(false);
                }else {//点击关闭
                    application.sendMsg("$change_SIM;auto;");
                    iv_sim2.setClickable(false);
                }
            }
        });
    }

    public void initView(){
        pb_conn = (ProgressBar) findViewById(R.id.pb_conn);
        ll_sim_auto = (LinearLayout) findViewById(R.id.ll_sim_auto);
        ll_sim1 = (LinearLayout) findViewById(R.id.ll_sim1);
        ll_sim2 = (LinearLayout) findViewById(R.id.ll_sim2);
        ll_setting = (LinearLayout) findViewById(R.id.ll_setting);
        ll_background = (LinearLayout) findViewById(R.id.ll_background);
        rl_package = (RelativeLayout) findViewById(R.id.rl_package);
        rl_gps_map = (RelativeLayout) findViewById(R.id.rl_gps_map);
        tv_sign_name = (TextView) findViewById(R.id.tv_sign_name);
        tv_sim1 = (TextView) findViewById(R.id.tv_sim1);
        tv_sim2 = (TextView) findViewById(R.id.tv_sim2);
        tv_auto = (TextView) findViewById(R.id.tv_auto);
        tv_gps = (TextView) findViewById(R.id.tv_gps);
        tv_include_tittle = (TextView) findViewById(R.id.tv_include_tittle);
        tv_sign_state = (TextView) findViewById(R.id.tv_sign_state);
        iv_toright2 = (ToggleView) findViewById(R.id.iv_toright2);
        tv_way_name = (ImageView) findViewById(R.id.tv_way_name);
        iv_auto = (ImageView) findViewById(R.id.iv_auto);
        iv_sim1 = (ImageView) findViewById(R.id.iv_sim1);
        iv_sim2 = (ImageView) findViewById(R.id.iv_sim2);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        tv_include_tittle.setText("移动网络设置");
        iv_auto.setOnClickListener(this);
        iv_sim1.setOnClickListener(this);
        iv_sim2.setOnClickListener(this);
        ll_sim1.setOnClickListener(this);
        ll_sim2.setOnClickListener(this);
        ib_back.setOnClickListener(this);
        rl_gps_map.setOnClickListener(this);
        rl_package.setOnClickListener(this);
        rl_package.getBackground().setAlpha(100);
        ll_setting.getBackground().setAlpha(100);



    }

    public void setSimState(boolean auto,boolean sim1,boolean sim2){
        if(auto){//打开状态
            iv_auto.setBackgroundResource(R.drawable.open);
            iv_sim1.setBackgroundResource(R.drawable.close);
            iv_sim2.setBackgroundResource(R.drawable.close);
        }
        if(sim1){//打开状态
            iv_sim1.setBackgroundResource(R.drawable.open);
            iv_auto.setBackgroundResource(R.drawable.close);
            iv_sim2.setBackgroundResource(R.drawable.close);
        }
        if(sim2){//打开状态
            iv_sim2.setBackgroundResource(R.drawable.open);
            iv_auto.setBackgroundResource(R.drawable.close);
            iv_sim1.setBackgroundResource(R.drawable.close);
        }
    }



    @Override
    protected void onResume() {//每个activity都要加这一句
        super.onResume();
        if(application==null)
            application = (BaseApplication) getApplication();
        application.setMsgListener(this);
        application.sendMsg("$current_SIM;");
        //设置信号信息
        String sim2_place = sp.getString("sim2_place", "未设置");
        tv_sim2.setText("SIM2"+"("+sim2_place+")");
        //设置流量单位
        String packageSetting = sp.getString("packageSetting", "4");
        boolean isGB = sp.getBoolean("isGB", false);
        if(isGB){
            tv_sign_state.setText(packageSetting+"GB套餐流量");
        }else {
            tv_sign_state.setText(packageSetting+"MB套餐流量");
        }
    }

        @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                this.finish();
                break;
            case R.id.rl_package:
                Intent intent=new Intent(NetWorkSetting.this,PackageSettingActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_sim1:
                Intent intent1=new Intent(NetWorkSetting.this,SimSettingActivity.class);
                intent1.putExtra("sim","sim1");
                startActivity(intent1);
                break;
            case R.id.ll_sim2:
                Intent intent2=new Intent(NetWorkSetting.this,SimSettingActivity.class);
                intent2.putExtra("sim","sim2");
                startActivity(intent2);
                break;
            case R.id.rl_gps_map:
                Intent intent3=new Intent(NetWorkSetting.this,IndoorLocationActivity.class);
//                Bundle bundle=new Bundle();
//                bundle.putDouble("gps1",gps1);
//                bundle.putDouble("gps2",gps2);
//                intent2.putExtras(bundle);
                intent3.putExtra("gps1",gps1);
                intent3.putExtra("gps2",gps2);

                startActivity(intent3);
                break;
        }
    }
    private static final long LOCATION_INFO = 5 * 1000;//5秒
    Handler handler;
    public void getLocation(){//每隔五秒发送数据
        if(handler==null){
            handler=new Handler();
        }
        handler.postDelayed(locationRunnable,LOCATION_INFO);
    }

    Runnable locationRunnable=new Runnable() {
        @Override
        public void run() {
                application.sendMsg("$current_location;");
            handler.postDelayed(this,LOCATION_INFO);
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        if(handler!=null && locationRunnable!=null)
         handler.removeCallbacks(locationRunnable);
    }

    @Override
    public void MessageGetListener(String msg) {
        try {
            if (msg != null) {
                String s =msg;
                application.makeToast(NetWorkSetting.this,s,false);
                ll_background.setVisibility(View.GONE);
                if (s.trim().length() > 0) {
                    Log.i(TAG, "mhandler接收到obj=" + s);
                    String s1[]=s.split(";");
                    if(s1[0].indexOf("up4g")!=-1){//打开
                        application.makeToast(NetWorkSetting.this,s,false);
                        if(s1[1].indexOf("up4g_done")!=-1){
                            ll_background.setVisibility(View.VISIBLE);
                            tv_way_name.setBackgroundResource(R.drawable.open);
                            isClose=false;
                            tv_way_name.setClickable(true);
                          //路由器需关闭wifi开关，需要断线重连
                            application.makeToast(NetWorkSetting.this,"WLAN连接正在打开",true);
                          String ssid = application.getSsid();
                          String password = application.getPassword();
                          WifiUtils wifiUtils=new WifiUtils(NetWorkSetting.this);
                          wifiUtils.WifiRebootForSetName(NetWorkSetting.this,application,ssid,password);

                        }else  if(s1[1].indexOf("cellData_expire")!=-1){
                            application.makeToast(NetWorkSetting.this,"流量超出，无法开启",true);
                        }

                    }else if(s1[0].indexOf("down4g")!=-1){//关闭
                        application.makeToast(NetWorkSetting.this,s,false);
                        tv_way_name.setBackgroundResource(R.drawable.close);
                        isClose=true;
                        tv_way_name.setClickable(true);
                    }else  if(s1[0].indexOf("init")!=-1){//首页数据
//          $12/31,50/70,55/70;  b0:e2:35:95:3f:82,RedmiNote3,38:bc:1a:eb:2b:73,Meizu;   4g_off;  700MB;
                        if(s1.length>4){
                            application.makeToast(NetWorkSetting.this,s1[4],false);
                            //运营商
                            String[] s_4g = s1[1].split(",");
                            if(s_4g[0].indexOf("CHINA MOBILE")!=-1){//中国移动
                                tv_sign_name.setText("中国移动");
                            }else  if(s_4g[0].indexOf("CHINA UNICOM")!=-1){//中国联通
                                tv_sign_name.setText("中国联通");
                            }else  if(s_4g[0].indexOf("CHINA TELECOM")!=-1){//中国电信
                                tv_sign_name.setText("中国电信");
                            }else {
                                tv_sign_name.setText("虚拟运营商");
                            }
                            String isOpen=s1[3];
                            if(isOpen.indexOf("off")!=-1){
                                tv_way_name.setBackgroundResource(R.drawable.close);
                                isClose=true;
                            }else {
                                tv_way_name.setBackgroundResource(R.drawable.open);
                                isClose=false;
                            }
                        }
                    }else if(s1[0].indexOf("current_SIM")!=-1){//关闭
                        SharedPreferences.Editor edit = sp.edit();
                        String current_SIM="sim1";
                        getLocation();
                        if(s1[1].indexOf("SIM_card1")!=-1){//SIM卡1
                            current_SIM="sim1";
                            isSim1Close=false;
                            isSim2Close=true;
                            isSimAutoClose=true;
                            setSimState(false,true,false);
                            tv_auto.setText("当前为手动模式，使用的SIM卡为：sim卡1");
                        }else if(s1[1].indexOf("SIM_card2")!=-1){//SIM卡2
                            current_SIM="sim2";
                            isSim2Close=false;
                            isSim1Close=true;
                            isSimAutoClose=true;
                            setSimState(false,false,true);
                            tv_auto.setText("当前为手动模式，使用的SIM卡为：sim卡2");
                        }else if(s1[1].indexOf("SIM_auto")!=-1){//自动切换
                            isSim2Close=true;
                            isSim1Close=true;
                            isSimAutoClose=false;
                            setSimState(true,false,false);
                            if(s1.length>2){
                                tv_auto.setVisibility(View.VISIBLE);
                                if(s1[2].indexOf("sim1")!=-1){
                                    current_SIM="sim1";
                                    tv_auto.setText("当前为自动模式，使用的SIM卡为：sim卡1");
                                }else if(s1[2].indexOf("sim2")!=-1){
                                    current_SIM="sim2";
                                    tv_auto.setText("当前为自动模式，使用的SIM卡为：sim卡2");
                                }
                            }
                        }
                        edit.putString("sim",current_SIM);
                        edit.commit();
                    }else if(s1[0].indexOf("change_SIM")!=-1){//设置sim卡切换模式
                        iv_sim2.setClickable(true);
                        iv_sim1.setClickable(true);
                        iv_auto.setClickable(true);
                        application.makeToast(NetWorkSetting.this,"模式切换成功",true);
                        if(s1[1].indexOf("auto")!=-1){
                            isSimAutoClose=false;
                            isSim1Close=true;
                            isSim2Close=true;
                            setSimState(true,false,false);
                            application.sendMsg("$current_SIM;");
                        }else if(s1[1].indexOf("sim1")!=-1){
                            tv_auto.setText("当前为手动模式，使用的SIM卡为：sim卡1");
                            isSimAutoClose=true;
                            isSim2Close=true;
                            isSim1Close=false;
                            setSimState(false,true,false);
                        }else if(s1[1].indexOf("sim2")!=-1){
                            tv_auto.setText("当前为手动模式，使用的SIM卡为：sim卡2");
                            isSimAutoClose=true;
                            isSim1Close=true;
                            isSim2Close=false;
                            setSimState(false,false,true);
                        }
                    }else if(s1[0].indexOf("current_location")!=-1){//gps信息
//                        11,120.679314,30.767166;\r\n"--省份编码，经度，纬度
                        String[] location = s1[1].split(",");
                        Double location_jd;
                        Double location_wd;
//                        if(location[1].indexOf("0")!=-1){//定位失败
                        if(Double.valueOf(location[1])==0){//定位失败
//                          120.679457,30.768363
                            gps1 = Double.valueOf("120.679457");
                            gps2 = Double.valueOf("30.768363");
                            tv_gps.setText("定位失败");
                        }else {//正常数据
                           location_jd = Double.valueOf(location[1]);
                           location_wd = Double.valueOf(location[2]);
                            gps1=location_jd;
                            gps2=location_wd;
                            String jd;
                            String wd;
                            if(location_jd>0){
                                jd=" 东经 ";
                            }else {
                                jd=" 西经 ";
                            }

                            if(location_wd>0){
                                wd=" 北纬 ";
                            }else {
                                wd=" 南纬 ";
                            }
                            tv_gps.setText("当前所处"+allProv[Integer.valueOf(location[0])]+"省"+jd+Math.abs(location_jd)+wd+Math.abs(location_wd));
                        }
                    }
                }
            }
            Log.i(TAG, "mhandler接收到msg" + msg);
        } catch (Exception ee) {
            Log.i(TAG, "加载过程出现异常");
            ee.printStackTrace();
        }
    }


}
